package org.dromara.answer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.answer.domain.vo.AiExamsVo;
import org.dromara.answer.domain.bo.AiExamsBo;
import org.dromara.answer.service.IAiExamsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 考试/测验
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/exams")
public class AiExamsController extends BaseController {

    private final IAiExamsService aiExamsService;

    /**
     * 查询考试/测验列表
     */
    @SaCheckPermission("answer:exams:list")
    @GetMapping("/list")
    public TableDataInfo<AiExamsVo> list(AiExamsBo bo, PageQuery pageQuery) {
        return aiExamsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出考试/测验列表
     */
    @SaCheckPermission("answer:exams:export")
    @Log(title = "考试/测验", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiExamsBo bo, HttpServletResponse response) {
        List<AiExamsVo> list = aiExamsService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷", AiExamsVo.class, response);
    }

    /**
     * 获取考试/测验详细信息
     *
     * @param examId 主键
     */
    @SaCheckPermission("answer:exams:query")
    @GetMapping("/{examId}")
    public R<AiExamsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long examId) {
        return R.ok(aiExamsService.queryById(examId));
    }

    /**
     * 新增考试/测验
     */
    @SaCheckPermission("answer:exams:add")
    @Log(title = "考试/测验", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiExamsBo bo) {
        return toAjax(aiExamsService.insertByBo(bo));
    }

    /**
     * 修改考试/测验
     */
    @SaCheckPermission("answer:exams:edit")
    @Log(title = "考试/测验", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiExamsBo bo) {
        return toAjax(aiExamsService.updateByBo(bo));
    }

    /**
     * 删除考试/测验
     *
     * @param examIds 主键串
     */
    @SaCheckPermission("answer:exams:remove")
    @Log(title = "考试/测验", businessType = BusinessType.DELETE)
    @DeleteMapping("/{examIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] examIds) {
        return toAjax(aiExamsService.deleteWithValidByIds(List.of(examIds), true));
    }
}
